package com.xinsoft.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.entity.dto.SysUserCountDTO;
import com.xinsoft.entity.po.SysUserCommonFun;
import com.xinsoft.entity.po.SysUserCount;
import com.xinsoft.service.SysCountConfigService;
import com.xinsoft.service.SysUserCountService;
import com.xinsoft.mapper.SysUserCountMapper;
import com.xinsoft.utils.TokenUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author hsm
* @description 针对表【sys_user_count(用户工作台设置)】的数据库操作Service实现
* @createDate 2023-11-24 15:22:53
*/
@Service
public class SysUserCountServiceImpl extends ServiceImpl<SysUserCountMapper, SysUserCount>
    implements SysUserCountService{


    @Resource
    private SysCountConfigService sysCountConfigService;

    @Override
    public List<SysUserCountDTO> findList() {
        QueryWrapper query = new QueryWrapper<SysUserCommonFun>().eq("user_id", TokenUtil.getUserId()).orderByAsc("sort");
        List<SysUserCount> list = list(query);
        List<SysUserCountDTO> result = new ArrayList<>();

        for (SysUserCount sysUserCount : list) {
            SysUserCountDTO record = new SysUserCountDTO();
            BeanUtils.copyProperties(sysUserCount,record);
            record.setSysCountConfig(sysCountConfigService.getById(sysUserCount.getCountId()));
            result.add(record);
        }
        return result;
    }

    @Override
    public void edit(List<SysUserCount> sysUserCountList) {
        //先删除原有设置
        QueryWrapper removeQuery = new QueryWrapper<SysUserCommonFun>().eq("user_id", TokenUtil.getUserId());
        remove(removeQuery);
        if(CollectionUtil.isNotEmpty(sysUserCountList)){
            for (SysUserCount sysUserCount : sysUserCountList) {
                sysUserCount.setUserId(TokenUtil.getUserId());
            }
        }
        saveBatch(sysUserCountList);
        //再保存最新的设置
    }
}




